<template>
  <div class="brands-page">
    <el-card class="page-card">
      <template #header>
        <div class="page-header">
          <h3>品牌管理</h3>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增品牌
          </el-button>
        </div>
      </template>

      <div class="card-body">
        <!-- 搜索表单 -->
        <el-form :model="searchObject" inline class="search-form">
          <el-form-item label="品牌编码">
            <el-input
              v-model="searchObject.code"
              placeholder="请输入品牌编码"
              clearable
              @keyup.enter="handleSearch"
            />
          </el-form-item>
          <el-form-item label="品牌名称">
            <el-input
              v-model="searchObject.name"
              placeholder="请输入品牌名称"
              clearable
              @keyup.enter="handleSearch"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">
              <el-icon><Search /></el-icon>
              搜索
            </el-button>
            <el-button @click="handleReset">
              <el-icon><Refresh /></el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>

        <!-- 表格容器 -->
        <div class="table-container">
          <el-table
            :data="pageData.data"
            @sort-change="changeTableSort"
            stripe
            v-loading="loading"
            height="100%"
          >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="logo" label="品牌Logo" width="100">
          <template #default="{ row }">
            <el-image
              v-if="row.logo"
              :src="row.logo"
              style="width: 50px; height: 50px"
              fit="cover"
            />
          </template>
        </el-table-column>
        <el-table-column prop="code" label="品牌编码" width="120" />
        <el-table-column prop="name" label="品牌名称" min-width="150" />
        <el-table-column prop="productCount" label="商品数量" width="100" />
        <el-table-column prop="sortNum" label="排序" width="80"  sortable="custom"/>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.state === 1 ? 'success' : 'danger'">
              {{ row.stateName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" text @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="danger" size="small" text @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
          </el-table>
        </div>

        <!-- 分页组件 -->
        <div class="pagination-container">
          <el-pagination
            v-model:current-page="searchObject.no"
            v-model:page-size="searchObject.size"
            :page-sizes="[10, 20, 50, 100]"
            :total="pageData.total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="sizeChange"
            @current-change="pageChange"
          />
        </div>
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <BrandFormDialog
      v-model="dialogVisible"
      :data="currentBrand"
      @success="handleFormSuccess"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus, Search, Refresh } from '@element-plus/icons-vue'
import { defineStore } from 'pinia'
import { usePage } from '@/uses/usePage.js'
import BrandFormDialog from './components/BrandFormDialog.vue'

const dialogVisible = ref(false)
const currentBrand = ref(null)

const searchStore = defineStore('brandStore', {

  state: () => {
    return {
      searchObject: {
        no: 1,
        size: 10,
        sortField: "id",
        sortMethod: "desc",
        shop: '',
        name: '',
        code: '',
        productCatalog: '',
        productGroup: ''
      }
    }
  }
})

const { searchObject } = searchStore()
const { pageData, searchList,sizeChange, pageChange, search, changeTableSort, loading } = usePage('/brand/search', searchObject)

const handleAdd = () => {
  currentBrand.value = null
  dialogVisible.value = true
}

const handleEdit = (row) => {
  currentBrand.value = { ...row }
  dialogVisible.value = true
}

const handleDelete = (row) => {
  ElMessage.success('删除成功')
  searchList();
}

const handleFormSuccess = (formData) => {
  // 这里可以处理表单提交成功后的逻辑，比如刷新列表
  console.log('保存的数据：', formData)
  searchList() // 刷新列表
}

// 搜索
const handleSearch = () => {
  searchObject.no = 1 // 重置到第一页
  searchList()
}

// 重置
const handleReset = () => {
  searchObject.name = ''
  searchObject.code = ''
  searchObject.no = 1
  searchList()
}
</script>

<style scoped>
.brands-page {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 5px;
  box-sizing: border-box;
}

.page-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.page-card :deep(.el-card__header) {
  padding: 12px 16px;
}

.page-card :deep(.el-card__body) {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 0;
}

.card-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 12px 16px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.page-header h3 {
  margin: 0;
  font-size: 16px;
}

.search-form {
  margin-bottom: 12px;
  padding: 12px 16px;
  background-color: #f5f7fa;
  border-radius: 4px;
  flex-shrink: 0;
}

.table-container {
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

.pagination-container {
  margin-top: 12px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}
</style>

